﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workshop;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using Yitter.IdGenerator;

namespace ProduceBasicData.Api.Application.Handler.Workshop
{
    /// <summary>
    /// 车间管理信息录入处理类
    /// </summary>
    public class UpdateWorkshopHandler : IRequestHandler<UpdateWorkshopCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Workshopmodel> shopRepo;
        private readonly IMapper mapper;

        public UpdateWorkshopHandler(IBaseRespository<Workshopmodel> shopRepo,
            IMapper mapper)
        {
            this.shopRepo = shopRepo;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(UpdateWorkshopCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var shops = await shopRepo.GetModelAsync(request.Id);
            if (shops==null)
            {
                res.Msg = "您要修改的数据不存在";
                res.Code=ApiEnum.Failed;
                return res;
            }

            var model = mapper.Map(request,shops);
            model.UpdateName=request.UpdateName;
            model.UpdateTime=DateTime.Now;

            

            var r=await shopRepo.Update(model);
            if (r>0)
            {
                res.Data = r;
                res.Code=ApiEnum.Success;
                res.Msg = "车间信息修改成功";
            }
            else
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "车间信息修改失败";
            }
            return res;
        }
    }
}
